import { defineStore } from "pinia";
interface IuserState {
  menus: Menu[];
  username:string
}
interface IMenuType {
  id: number;
  parentId: number;
  title:string;
  icon?:string;
  hidden:number;
  name:string;
  children?: IMenuType[];
}
export const useUserStore = defineStore("user", {
  state: (): IuserState => ({
    menus: [],
    username:""
  }),
  getters: {
    getMenusRoutes(state) {
      // IMenuType[]
      const newMenus: {[k:number]:IMenuType} = {};
      state.menus.forEach((item) => {
        if (item.parentId == 0) {
          newMenus[item.id] = {
            ...item,
            children: newMenus[item.id] ? newMenus[item.id].children : [],
          };
        } else {
          newMenus[item.parentId] ??= {} as IMenuType;
          newMenus[item.parentId].children ??= [];
          newMenus[item.parentId].children!.push(item);
        }
      });
      // console.log("菜单",newMenus);
      return newMenus
    },
    getPermitions(state){
      return state.menus.map(item=>item.name)
    }
  },
  actions: {
    updateMenus(menus: Menu[]) {
      //不能将类型{)[]”分配给类型never[]”
      this.menus = menus;
    },
  },
});
